package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.vo.CourseWebVoOrder;
import com.atguigu.commonutils.vo.UcenterMemberOrder;
import com.atguigu.eduorder.client.CourseClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-28
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    
    @Autowired
    private CourseClient courseClient;
    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 新增订单
     * @param courseId
     * @param memberIdByJwtToken
     * @return
     */
    @Override
    public String createOrders(String courseId, String memberIdByJwtToken) {
        Order order = new Order();
        String orderNo = OrderNoUtil.getOrderNo();

        //用户信息
        UcenterMemberOrder ucenterInfoOrder = ucenterClient.getUcenterInfoOrder(memberIdByJwtToken);
        //课程信息
        CourseWebVoOrder courseInfoOrder = courseClient.getCourseInfoOrder(courseId);

        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseId); //课程id
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(ucenterInfoOrder.getMobile());
        order.setNickname(ucenterInfoOrder.getNickname());
        order.setStatus(0);  //订单状态（0：未支付 1：已支付）
        order.setPayType(1);  //支付类型 ，微信1

        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }

    /**
     * 根据orderNo获取用户信息
     * @param orderNo
     * @return
     */
    @Override
    public Order getOrder(String orderNo) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        Order order = baseMapper.selectOne(queryWrapper);
        return order;
    }

    /**
     * 是否购买了课程
     * @param courseId
     * @param memberId
     * @return
     */
    @Override
    public boolean isBuyCourse(String courseId, String memberId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("status",1);

        Integer count = baseMapper.selectCount(queryWrapper);
        if(count>0)
        {
            return true;
        }
        return false;
    }
}
